import { createApp } from 'vue'
import App from './App.vue'

// 以下是原来的代码（已注销）
/*
import router from './router'

let app
function render(props) {
    app = createApp(App)
    app.use(router)
    const container = props.container
    app.mount(container ? container.querySelector('#app') : document.getElementById('app'))
}
*/

// 以下是新的实现
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from './views/HomeView.vue'
import AboutView from './views/AboutView.vue'

let app
let router

// 定义路由配置
const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    component: AboutView
    
  }
]

function render(props) {
    // 根据是否在qiankun环境中运行来动态设置路由的base路径
    const base = window.__POWERED_BY_QIANKUN__ ? '/vue3' : process.env.BASE_URL
    
    // 创建新的router实例
    router = createRouter({
      history: createWebHistory(base),
      routes
    })
    
    app = createApp(App)
    app.use(router)
    const container = props.container
    app.mount(container ? container.querySelector('#app') : document.getElementById('app'))
}

if (!window.__POWERED_BY_QIANKUN__) {
    render({})
}

export async function bootstrap(props) {
    console.log('[vue3] vue3 app bootstraped')
}

export async function mount(props) {
    console.log('[vue3] vue3 app mount', props)
    render(props)
}

export async function unmount(props) {
    console.log(props);
    app.unmount();
    if (router && router.history && typeof router.history.destroy === "function") {
        router.history.destroy();
    }
    app = null
}